package com.starry.module.system.core.oauth2.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.RandomUtil;
import com.starry.core.common.enums.UserTypeEnum;
import com.starry.core.security.domain.BaseUser;
import com.starry.db.core.enums.DBEnableDisableStatusEnum;
import com.starry.module.system.core.member.entity.MemberUser;
import com.starry.module.system.core.member.service.MemberUserService;
import com.starry.module.system.core.oauth2.entity.Oauth2ThirdAccount;
import com.starry.module.system.core.oauth2.entity.Oauth2ThirdUser;
import com.starry.module.system.core.oauth2.mapper.Oauth2ThirdAccountMapper;
import com.starry.module.system.core.oauth2.service.Oauth2ThirdAccountService;
import com.starry.module.system.core.oauth2.service.Oauth2ThirdUserService;
import com.starry.module.system.core.user.entity.SysUser;
import com.starry.module.system.core.user.service.SysUserService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 第三方用户-服务实现
 *
 * @author xiaoke
 */
@Service("oauth2ThirdAccountService")
public class Oauth2ThirdAccountServiceImpl implements Oauth2ThirdAccountService {

    private final Oauth2ThirdAccountMapper oauth2ThirdAccountMapper;
    private final SysUserService sysUserService;
    private final Oauth2ThirdUserService oauth2ThirdUserService;
    private final MemberUserService memberUserService;

    public Oauth2ThirdAccountServiceImpl(Oauth2ThirdAccountMapper oauth2ThirdAccountMapper, SysUserService sysUserService, Oauth2ThirdUserService oauth2ThirdUserService, MemberUserService memberUserService) {
        this.oauth2ThirdAccountMapper = oauth2ThirdAccountMapper;
        this.sysUserService = sysUserService;
        this.oauth2ThirdUserService = oauth2ThirdUserService;
        this.memberUserService = memberUserService;
    }

    @Override
    public SysUser getByOpenIdAndType(String openId, String type) {
        Oauth2ThirdUser thirdAccount = oauth2ThirdUserService.get(openId, type, UserTypeEnum.ADMIN.getValue());
        if (thirdAccount == null) return null;
        return sysUserService.getById(thirdAccount.getUserId());
    }

    @Override
    public MemberUser getMemberByOpenIdAndType(String openId, String type) {
        Oauth2ThirdUser thirdAccount = oauth2ThirdUserService.get(openId, type, UserTypeEnum.MEMBER.getValue());
        if (thirdAccount == null) return null;
        return memberUserService.getById(thirdAccount.getUserId());
    }

    @Transactional
    @Override
    public SysUser initThirdAccount(Oauth2ThirdAccount thirdAccount) {
        // 保存第三方账号信息
        oauth2ThirdAccountMapper.insert(thirdAccount);

        BaseUser user = initBaseUser(thirdAccount);

        // 初始化用户信息
        SysUser sysUser = BeanUtil.toBean(user, SysUser.class);


        // 默认启用
        sysUser.setStatus(DBEnableDisableStatusEnum.ENABLE.getCode());

        sysUser = sysUserService.initialUser(sysUser);

        user.setId(sysUser.getId());

        // 保存第三方用户关联
        saveThirdUser(thirdAccount, user, UserTypeEnum.ADMIN);

        return sysUser;
    }

    @Override
    public MemberUser initMemberThirdAccount(Oauth2ThirdAccount thirdAccount) {
        // 保存第三方账号信息
        oauth2ThirdAccountMapper.insert(thirdAccount);

        BaseUser user = initBaseUser(thirdAccount);

        // 初始化用户信息
        MemberUser memberUser = BeanUtil.toBean(user, MemberUser.class);

        user.setId(memberUser.getId());

        memberUser.setStatus(DBEnableDisableStatusEnum.ENABLE.getCode());

        memberUser = memberUserService.initialUser(memberUser);

        user.setId(memberUser.getId());

        // 保存第三方用户关联
        saveThirdUser(thirdAccount, user, UserTypeEnum.MEMBER);

        return memberUser;
    }


    /**
     * 初始化用户信息
     *
     * @param thirdAccount 第三方账号信息
     * @return 用户信息
     */
    private BaseUser initBaseUser(Oauth2ThirdAccount thirdAccount) {
        // 初始化用户信息
        BaseUser user = new BaseUser();

        if (!ObjectUtil.isAllNotEmpty(thirdAccount.getUsername())) {
            user.setUsername(RandomUtil.randomString(8));
        } else {
            user.setUsername(thirdAccount.getUsername());
        }

        if (!ObjectUtil.isAllNotEmpty(thirdAccount.getAvatar())) {
            user.setAvatar("");
        } else {
            user.setAvatar(thirdAccount.getAvatar());
        }

        if (!ObjectUtil.isAllNotEmpty(thirdAccount.getName())) {
            user.setName(user.getUsername());
        } else {
            user.setName(thirdAccount.getName());
        }
        return user;
    }

    /**
     * 保存第三方用户关联
     *
     * @param thirdAccount 第三方账号信息
     * @param user         第三方用户信息
     * @param userType     用户类型
     */
    private void saveThirdUser(Oauth2ThirdAccount thirdAccount, BaseUser user, UserTypeEnum userType) {
        // 保存第三方用户关联
        Oauth2ThirdUser oauth2ThirdUser = new Oauth2ThirdUser();
        oauth2ThirdUser.setOpenid(thirdAccount.getOpenid());
        oauth2ThirdUser.setThirdType(thirdAccount.getType());
        oauth2ThirdUser.setUserId(user.getId());
        oauth2ThirdUser.setUserType(userType.getValue());
        oauth2ThirdUserService.save(oauth2ThirdUser);
    }
}

